<?php

namespace Modules\Base\Repository;

use Modules\Base\Models\BoxNumber;
use App\Enums\BoxFrom;
use App\Enums\InvTransDocType;
use App\Enums\InvTransType;
use Modules\Inventory\Repository\InventoryTransRepository;

class BoxNumberRepository
{

    /**
     * 根据箱号编码查询生产汇单创建的箱号信息
     * @param $code
     * @return mixed
     */
    static public function getReportOrderBoxByCode($code){
       return BoxNumber::where('code',$code)->where('from',BoxFrom::REPORT_ORDER->value)->first();
    }

    /**
     * 根据箱号查询箱号信息
     * @param $code
     * @return mixed
     */
    static public function getBoxByCode($code){
        return BoxNumber::where('code',$code)->first();
    }

    /**
     * 获取箱号操作记录
     * @param $lotnum
     */
    static public function getBoxLog($lotnum){

        $logList = InventoryTransRepository::getTransByLotnum($lotnum);
        $returnList = [];
        foreach ($logList as $item){

            $transType  = InvTransType::tryFrom($item->trans_type);
            $docType    = InvTransDocType::tryFrom($item->doc_type);

            $wms        = InvTransType::INBOUND->value == $item->trans_type ? $item->to_wms : $item->from_wms;
            $location   = InvTransType::INBOUND->value == $item->trans_type ? $item->to_location : $item->from_location;
            $lotnum     = InvTransType::INBOUND->value == $item->trans_type ? $item->to_lotnum : $item->from_lotnum;

            $returnList[] = [
                'doc_type'      => $docType ? $docType->label() : '',
                'docno'         => $item->docno,
                'trans_type'    => $transType ? $transType->label() : '',
                'material_no'   => $item->material_no,
                'qty'           => $item->qty,
                'wms_code'      => $wms,
                'location'      => $location,
                'lotnum'        => $lotnum,
                'creator'       => $item->creator_id,
                'add_time'      => $item->created_at,
            ];
        }

        return $returnList;

    }
}
